//
//  KZPrepareExamVC.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2018/12/26.
//  Copyright © 2018 zhukai. All rights reserved.
//

#import "KZReviewExamMoreVC.h"

// vc

// view
#import "KZReviewInformationView.h"         // 信息视图
#import "KZReviewSearchAndFilterView.h"     // 搜索和筛选
#import "KZFilterView.h"                    // 筛选视图

// cell
#import "KZReviewTestPaperCell.h"           // 试卷


@interface KZReviewExamMoreVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) KZReviewInformationView *reviewInformationView;
@property (nonatomic, strong) KZReviewSearchAndFilterView *searchFilterView;


@end

@implementation KZReviewExamMoreVC
{
    // 记录上次选中的筛选标签
    NSArray *_filterSelectedTags;
}

#pragma mark - vc生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"分类试卷";
    [self setupUI];
    [self addLeftBackBarButtonItem];
}

#pragma mark - 视图
- (void)setupUI {
    
    // 列表
    _table = [[UITableView alloc] init];
    _table.estimatedRowHeight = 200;
    _table.rowHeight = UITableViewAutomaticDimension;
    _table.backgroundColor = kBgColor;
    _table.tag = 666;
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    [_table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0)).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
    
    [_table registerClass:[KZReviewTestPaperCell class] forCellReuseIdentifier:@"paperCell"];
    
    // 下拉刷新和上拉加载
    _table.mj_header = [KZRefreshHeader headerWithRefreshingBlock:^{
        NSLog(@"刷新");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.table.mj_header endRefreshing];
        });
    }];
    _table.mj_footer = [KZRefreshFooter footerWithRefreshingBlock:^{
        NSLog(@"加载更多");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.table.mj_footer endRefreshingWithNoMoreData];
        });
    }];

}

#pragma mark - UITableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KZReviewTestPaperCell *cell = [tableView dequeueReusableCellWithIdentifier:@"paperCell"];
    cell.testStatus = indexPath.row % 3;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 60;
    } else {
        return 0;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    // 搜索和筛选
    _searchFilterView = [KZReviewSearchAndFilterView new];
    @weakify(self);
    _searchFilterView.onTouchFilterCallback = ^{
        @strongify(self);
        // 开始筛选
        [self beginFilter];
    };
    return _searchFilterView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:false];
}


#pragma mark - 事件


#pragma mark - 辅助方法
// 开始筛选
- (void)beginFilter {
    
    KZFilterView *filterView = [KZFilterView new];
    filterView.lateseSelectedTags = self->_filterSelectedTags;
    filterView.tag = 777;
    filterView.onConfirmCallback = ^(NSArray *selectedTags) {
        self->_filterSelectedTags = selectedTags;
        self->_searchFilterView.isInFilter = selectedTags != nil;
    };

    CGFloat y = [self.table convertPoint:self.searchFilterView.origin toView:self.view].y;
    if (self.table.scrollEnabled) {
        [UIView animateWithDuration:0.25 animations:^{
            self.table.contentOffset = CGPointMake(0, self.table.contentOffset.y + y);
        } completion:^(BOOL finished) {
            [filterView showAtY:self.searchFilterView.height basalView:self.view];
        }];
    } else {
        KZFilterView *filterView = [self.view viewWithTag:777];
        [filterView hide];
    }
}

@end
